--- title: DataBunch for Super Resolution keywords: fastai sidebar: home_sidebar ---
%reload_ext autoreload
%autoreload 2
%matplotlib inline
import os
import cv2
import numpy as np
import re
import random
from tqdm import tqdm
from matplotlib import pyplot as plt
import sys
sys.path.append('..')
from superres.datasets import *
from superres.srcnn import *
#from psnr.psnr import *
seed = 8610
random.seed(seed)
np.random.seed(seed)
il = ImageImageList.from_folder(set14_hr, after_open=partial(resize_image, size=64))
print(il)
il[0].show()
# scale=4
il = ImageImageList.from_folder(set14_hr, after_open=partial(lr_image, scale=4))
print(il)
il[0].show()
# scale=4, resize=True
il = ImageImageList.from_folder(set14_hr, after_open=partial(lr_image, scale=4, sizeup=True))
print(il)
il[0].show()
# scale=4, resize=True, size=256
il = ImageImageList.from_folder(set14_hr, after_open=partial(lr_image, scale=4, sizeup=True, size=256))
print(il)
il[0].show()
il = ImageImageList.from_folder(set14_hr, after_open=partial(crop_center_image, size=256))
print(il)
il[0].show()
il = ImageImageList.from_folder(set14_hr, convert_mode='YCbCr', after_open=split_luminance)
print(il)
il[0].show(cmap='gray')
# Original
il = ImageImageList.from_folder(set14_hr)
print(il)
il[0].show()
# y when testing:
il = ImageImageList.from_folder(set14_hr, after_open=partial(after_open_image, size=256))
print(il)
il[0].show()
# x when training: if x.shape == y.shape
il = ImageImageList.from_folder(set14_hr, after_open=partial(after_open_image, size=256, scale=4, sizeup=True))
print(il)
il[0].show()
# x when training: if x.shape == y.shape // scale
il = ImageImageList.from_folder(set14_hr, after_open=partial(after_open_image, size=64, scale=4))
print(il)
il[0].show()
# x when training: if x.shape == y.shape and channel is luminance only
il = ImageImageList.from_folder(set14_hr, after_open=partial(after_open_image, size=256, scale=4, sizeup=True, luminance=True))
print(il)
il[0].show(cmap='gray')
doc(get_transforms)
get_sr_transforms(size=256)
tfms = get_sr_transforms(256)
il = ImageImageList.from_folder(set14_hr)
print(il)
img = il[0]
img.show()
img.apply_tfms(tfms[0]).show()
# in_size=64, out_size=256
data = create_sr_databunch(div2k_train_hr_crop_256, in_size=64, out_size=256, scale=4, bs=10, seed=8610)
print(data)
data.show_batch()
# in_size=256, out_size=256
data = create_sr_databunch(div2k_train_hr_crop_256, in_size=256, out_size=256, scale=4, bs=10, seed=8610)
print(data)
data.show_batch()
# in_size=256, out_size=256, luminance only
data = create_sr_databunch(div2k_train_hr_crop_256, in_size=256, out_size=256, scale=4, bs=10, convert_mode='YCbCr', seed=8610)
print(data)
data.show_batch(cmap='gray')
def create_imagenet_databunch(data_path:PosixPath, in_size:int, out_size:int, scale:int, bs:int, convert_mode:str='RGB', seed:int=1234)->'DataBunch':
""" create databunch for super-resolution """
luminance = convert_mode == 'YCbCr'
sizeup = in_size == out_size
src = (ImageImageList.from_folder(data_path, convert_mode=convert_mode,
after_open=partial(after_open_image, size=in_size, scale=scale, sizeup=sizeup, luminance=luminance))
.split_by_rand_pct(seed=seed)
.label_from_func((lambda x: x), label_cls=ImageImageList, convert_mode=convert_mode,
after_open=partial(after_open_image, size=out_size, luminance=luminance)))
data = (src.transform(get_sr_transforms(size=in_size), tfm_y=True)
.transform_y(get_sr_transforms(size=out_size, max_lighting=0))
.databunch(path=Path('.'), bs=bs))
# set imagenet_status
data.normalize(imagenet_stats, do_y=True)
return data
data = create_imagenet_databunch(div2k_train_hr_crop_256, in_size=256, out_size=256, scale=4, bs=10, convert_mode='YCbCr', seed=8610)
# get one_batch
x, y = data.one_batch()
# the channel is 3 instead of 1.
x.shape
# the channel is 3 instead of 1.
x, y = next(iter(data.train_dl))
x.shape
# the shape of data.x is correct.
print(data.x)
data.x[0].shape
# the shape of data.x is correct.
print(data.train_ds[0])
x, y = data.train_ds[0]
x.shape
# train_dl class is ...
type(data.train_dl)
class DeviceDataLoader():
def __iter__(self):
"Process and returns items from `DataLoader`."
for b in self.dl: yield self.proc_batch(b)
# the shape of train_dl.dl(Pytorch class) is correct.
x, y = next(iter(data.train_dl.dl))
x.shape
# The shape changes when you call DeviceDataLoader.proc_batch()
b = next(iter(data.train_dl.dl))
x, y = data.train_dl.proc_batch(b)
x.shape
class DeviceDataLoader():
def proc_batch(self,b:Tensor)->Tensor:
"Process batch `b` of `TensorImage`."
b = to_device(b, self.device)
for f in listify(self.tfms): b = f(b)
return b
# The shape is changed in _normalize_batch
data.train_dl.tfms
fastai.vidion.data.py
def normalize(x:TensorImage, mean:FloatTensor,std:FloatTensor)->TensorImage:
"Normalize `x` with `mean` and `std`."
return (x-mean[...,None,None]) / std[...,None,None]
def _normalize_batch(b:Tuple[Tensor,Tensor], mean:FloatTensor, std:FloatTensor, do_x:bool=True, do_y:bool=False)->Tuple[Tensor,Tensor]:
"`b` = `x`,`y` - normalize `x` array of imgs and `do_y` optionally `y`."
x,y = b
mean,std = mean.to(x.device),std.to(x.device)
if do_x: x = normalize(x,mean,std)
if do_y and len(y.shape) == 4: y = normalize(y,mean,std)
return x,y
The shape changed because .normalize(imagenet_stats) was specified when creating a DataBunch. So I don't specify imagenet_stats for 1 channel-image.
train_hr = div2k_train_hr_crop_256
in_size = 256
out_size = 256
scale = 4
bs = 10
data = create_sr_databunch(train_hr, in_size=in_size, out_size=out_size, scale=scale, bs=bs, seed=seed)
print(data)
data.show_batch()
Image super-resolution using deep convolutional networks
https://arxiv.org/abs/1501.00092
31 Dec 2014
model = SRCNN()
loss_func = MSELossFlat()
metrics = [m_psnr]
learn = Learner(data, model, loss_func=loss_func, metrics=metrics)
learn.path = Path('.')
model_name = model.__class__.__name__
lr_find(learn)
learn.recorder.plot(suggestion=True)
lr = 1e-3
lrs = slice(lr)
epoch = 3
pct_start = 0.3
wd = 1e-3
save_fname = model_name
callbacks = [ShowGraph(learn), SaveModelCallback(learn, name=save_fname)]
learn.fit_one_cycle(epoch, lrs, pct_start=pct_start, wd=wd, callbacks=callbacks)
learn.show_results()
test_hr = set14_hr
il_test_x = ImageImageList.from_folder(test_hr, after_open=partial(after_open_image, scale=4, sizeup=True, size=out_size))
il_test_y = ImageImageList.from_folder(test_hr, after_open=partial(after_open_image, size=out_size))
_ = learn.load(save_fname)
sr_test(learn, il_test_x, il_test_y, model_name)
# Official: bicubic: PSNR:25.99, SSIM:0.7486
# Official: SRCNN: PSNR:27.50, SSIM:0.7513